// pages/truckCenter/fromAddress.js
let _page, _this;
import {
  $Message
} from '../../components/base/index'
import commonUtils from '../../utils/util.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainDriverName:'',
    mainDriverPhone:'',
    fromProvinceName:'',
    fromCityName:'',
    fromCountyName:'',
    fromAddressDetail:'',
    addressStr:'',
  },
  getFromAddress(){
    _this = this;
    wx.chooseLocation({
      success: (res) => {
        const {latitude,longitude} = res;
        console.log(res);
        if(latitude&&longitude){
          _this.getRealLocation(latitude,longitude);
        }
      },
    })
  },
  //根据经纬度获取省市县，详细地址
  getRealLocation(latitude, longitude){
    _page = this;
    wx.request({
      url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=VEJBZ-M5YLX-KZN4O-7VWPS-VGOFT-46FXA`,
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        console.log(res);
        //获取省市县
        const {city,province,district}=res.data.result.address_component;
        //获取详细地址
        const {recommend} = res.data.result.formatted_addresses;
        _page.setData({
          fromProvinceName:province,
          fromCityName:city,
          fromCountyName:district,
          fromAddressDetail:recommend,
          addressStr:recommend,
        })
      }
    })
  },
  submitForm(formData){
    const {detail:{value}} = formData;
    const info = {
      fromProvinceName:this.data.fromProvinceName,
      fromCityName:this.data.fromCityName,
      fromCountyName:this.data.fromCountyName,
      fromAddressDetail:this.data.fromAddressDetail,
      ...value,
    }
    //校验数据
    if(this.checkAddressInfo(info)){
      const {orderType,addressType} = this.data;
      if(orderType==='truckCenter'){
        wx.setStorageSync('addressInfo', info);
        wx.navigateTo({
          url: '/pages/truckCenter/addv2',
        })
      }else if(orderType==='orderCenter'){
        if(addressType==='FA'){
          wx.setStorageSync('orderFromAddress', info);
        }else if(addressType==='TA'){
          wx.setStorageSync('orderToAddress', info);
        }
        wx.navigateTo({
          url: '/pages/orderCenter/addv2',
        })
      }
    }
  },
  checkAddressInfo(formData){
    if(formData.fromProvinceName===''||formData.fromCityName===''
        ||formData.fromCountyName===''||formData.fromAddressDetail===''){
      this.showErrorMessage('起运地信息不能为空');
        return false;
    }
    if (formData.mainDriverName === '') {
      this.showErrorMessage('姓名不能为空');
      return false;
    }
    if (commonUtils.isEmoji(formData.mainDriverName)) {
      this.showErrorMessage('姓名不符合要求');
      return false;
    }
    if(formData.mainDriverPhone===''){
      this.showErrorMessage('联系方式不能为空');
      return false;
    }
    if (!commonUtils.checkTelphoneNumber(formData.mainDriverPhone)) {
      this.showErrorMessage('联系方式格式不正确');
      return false;
    }
    return true;
  },
  showErrorMessage(msg){
    $Message({
      content: msg,
      type: 'error'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {orderType,addressType} = options;
    this.setData({
      orderType,addressType
    })
    if(addressType && addressType==='TA'){
      wx.setNavigationBarTitle({
        title: '收货人信息',
      })
    }
    if(orderType==='truckCenter'){
      const addressInfo = wx.getStorageSync('addressInfo');
      if(addressInfo&&addressInfo.fromAddressDetail){
        this.setData({
          ...addressInfo,
          addressStr:addressInfo.fromAddressDetail,
        })
      }
    }else if(orderType==='orderCenter'){
      if(addressType==='FA'){
        const orderFromAddress = wx.getStorageSync('orderFromAddress');
        if(orderFromAddress&&orderFromAddress.fromAddressDetail){
          this.setData({
            ...orderFromAddress,
            addressStr:orderFromAddress.fromAddressDetail,
          })
        }
      }else if(addressType==='TA'){
        const orderToAddress = wx.getStorageSync('orderToAddress');
        if(orderToAddress&&orderToAddress.fromAddressDetail){
          this.setData({
            ...orderToAddress,
            addressStr:orderToAddress.fromAddressDetail,
          })
        }
      }
    }
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})